import { interpreter } from "@/api/request";
import { defineStore } from "pinia";
import { ref } from "vue";
import { getCaseTypeList } from "@/api/zixun";
import debounce from "@/utils/debounce";
interface Info {
  avatar_image: string;
  created_at: string;
  id: number;
  message_total: number;
  nickname: string;
  opening_remarks: string;
}
export const useTabs = defineStore("tabs", () => {
  const list = ref<Info[]>([]);

  const getTabList = async () => {
    if (!list.value.length) {
      const [err, res] = await interpreter(getCaseTypeList());
      if (!err) {
        list.value = res.data;
      }
    }
  };

  const changeTab = debounce((id: number, canChange = true) => {
    if (!canChange) {
      uni.showToast({
        title: "正在对话中，请勿切换",
        icon: "none"
      });
      return;
    }
    const findIndex = list.value.findIndex((r: any) => r.id === id);
    if (findIndex > -1) {
      uni.navigateTo({
        url: `/pages/index/index?id=${id}`
      });
    }
  });

  const getTabById = async (id: number) => {
    await getTabList();
    return list.value.find(item => item.id === id);
  };

  return {
    list,
    getTabById,
    changeTab,
    getTabList
  };
});
